'use strict';

var vue = new Vue({
    data: {
        loading: false,
        captcha_src: '',
        captcha_md5: '',
        form: {
            username: 'admin',
            password: '123456',
            code: '',
            captcha_key: ''
        }

    },
    methods: {
        reload_captcha: function reload_captcha() {
            var _this = this;

            axios.post('/api/image_captcha/index', {}).then(function (response) {
                //console.log(response);
                var data = response.data;
                _this.captcha_src = data.data.base64.replace('\r\n', '');
                _this.form.captcha_key = data.data.key;
                _this.form.code = '';
                _this.captcha_md5 = data.data.md5;
            }).catch(function (error) {
                _this.$message.error(error.toString());
            });
        },
        onSubmit: function onSubmit() {
            var _this2 = this;

            //console.log(this.form);
            if (!this.form.username) {
                this.$message.error('账号不能为空');
                this.$refs.username.focus();
                return;
            }
            if (!this.form.password) {
                this.$message.error('密码不能为空');
                this.$refs.password.focus();
                return;
            }
            if (!this.form.code) {
                this.$message.error('验证码不能为空');
                this.$refs.code.focus();
                return;
            }
            if (md5(this.form.code) != this.captcha_md5) {
                this.$message.error('验证码不正确');
                this.$refs.code.value = '';
                this.$refs.code.focus();
                return;
            }

            this.loading = true;
            axios.post('/api/welcome/login', this.form).then(function (response) {
                //console.log(response);
                var data = response.data;
                _this2.loading = false;
                if (data.code > 0) {
                    //cookie
                    Cookies.set('userid', data.data.userid);
                    Cookies.set('username', data.data.username);
                    Cookies.set('groupid', data.data.groupid);
                    Cookies.set('auth', data.data.auth);
                    Cookies.set('power', JSON.stringify(data.data.power));
                    _this2.$alert(data.msg, '系统提醒', {
                        confirmButtonText: '确定',
                        callback: function callback(action) {
                            //跳转到会员中心首页
                            location.replace('/admin/home/index');
                        }
                    });
                } else {
                    _this2.$alert(data.msg, '系统提醒', {
                        confirmButtonText: '确定',
                        callback: function callback(action) {
                            try {
                                if (data.element) {
                                    _this2.$refs[data.element].value = '';
                                    _this2.$refs[data.element].focus();
                                }
                            } catch (e) {}
                        }
                    });
                }
            }).catch(function (error) {
                _this2.loading = false;
                _this2.$message.error(error.toString());
            });
        }
    },
    created: function created() {},
    mounted: function mounted() {
        if (typeof Cookies.get('auth') != 'undefined') {
            location.replace('/admin/home/index');
        }
        //加载验证码
        this.reload_captcha();
    }
}).$mount('#app');